package com.sinoglobal.ningxia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.ActsAdapter;
import com.sinoglobal.ningxia.adapter.EnentsListViewAdapter;
import com.sinoglobal.ningxia.beans.Act_list;
import com.sinoglobal.ningxia.beans.EnentsListVo;
import com.sinoglobal.ningxia.beans.Type;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.MContants;

import java.util.ArrayList;

/**
 * 
 * @author zbl
 * @createdate 2014-6-23 下午5:42:45
 * @Description:主界面“活动”
 */
public class EnentsActivity extends BaseActivity implements IBase, AbOnListViewListener {
	private RelativeLayout enentsSelect;
	private TextView enentAll;// 全部
	private CheckBox enentChooseContent;// 选择查看内容
	private ListView enents;// 筛选列表
	private ArrayList<Type> typeList;// 活动的删选形式，如："全部", "优惠", "活动"
	private ArrayList<Type> copyTypeList = new ArrayList<Type>();
	private EnentsListViewAdapter enentsListViewAdapter;// 筛选列表的adapter
	private ArrayList<Act_list> actList;// 活动的具体列表项
	private ActsAdapter eAdapter;// 内容的适配器
	private AbPullListView activityListView;// 活动内容的ListView
	private int pageNum;// 活动的页数
	private String type;// 活动形式
	private RelativeLayout noGoods;
	private boolean isFirst = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isTemplate = true;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enents);
		openChenJInShi();//开启沉浸式
		titleView.setText(R.string.events);
		init();// 初始化控件
		loadInfo(type, pageNum, true);// 访问网络获取数据
		showListener();
	}
	//获取活动列表
	public void loadInfo(final String type, final int pageNum, boolean tag1) {
		new ItktAsyncTaskWithDialog<Integer, Void, EnentsListVo>(getString(R.string.loading), tag1, true) {

			@Override
			public EnentsListVo before(Integer... params) throws Exception {
				return RemoteImpl.getInstance().getEnentsListyVo(type, params[0]);
			}

			@Override
			public void after(EnentsListVo result) {
				if (!result.getTypelist().isEmpty()) {
					noGoods.setVisibility(View.GONE);
					if (Code.CODE_SUCCESS != result.getCode()) {
						showShortToastMessage(result.getMessage());
					} else {

						if (pageNum == 0) {
							actList.clear();
							typeList = result.getTypelist();
							// 判断是否为第一次进入该页面，本过程只进行一次
							if (isFirst) {
								copyTypeList.clear();
								copyTypeList.addAll(typeList);
								for (int i = 0; i < copyTypeList.size(); i++) {
									if (getString(R.string.act_listview_itemTitle).equals(copyTypeList.get(i).getType_name())) {
										copyTypeList.remove(i);
									}
								}
								enentsListViewAdapter.addData(copyTypeList);
								enentsListViewAdapter.notifyDataSetChanged();
								isFirst = false;
							}
							activityListView.stopRefresh();
							activityListView.setPullLoadEnable(true);
						} else {
							activityListView.stopLoadMore();
						}
						if (!result.getAct_list().isEmpty()) {
							actList.addAll(result.getAct_list());
							eAdapter.addData(actList);
							// 从服务器获得的数据通过显示到列表上
							eAdapter.notifyDataSetChanged();
						} else {
							eAdapter.notifyDataSetChanged();
							activityListView.setPullLoadEnable(false);
							showShortToastMessage(getString(R.string.noMoreMessage));
						}
					}
				} else {
					noGoods.setVisibility(View.VISIBLE);
					activityListView.setVisibility(View.GONE);
				}

			}

			@Override
			public void exception() {

			}
		}.execute(pageNum);

	}

	public void init() {
		enentsSelect = (RelativeLayout) findViewById(R.id.enents_select);
		enentAll = (TextView) findViewById(R.id.enent_tv);
		enentChooseContent = (CheckBox) findViewById(R.id.enent_checkBox);
		enents = (ListView) findViewById(R.id.enents_listView);
		activityListView = (AbPullListView) findViewById(R.id.ab_listView1);
		noGoods = (RelativeLayout) findViewById(R.id.no_avts);

		// 用来设置AndBase AbPullListView 的下拉和加载更多样式
		activityListView.getHeaderView().setHeaderProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		activityListView.getFooterView().setFooterProgressBarDrawable(this.getResources().getDrawable(R.drawable.progress_circular));
		// 设置从第一页开始请求活动
		pageNum = 0;
		type = MContants.EMPTY_STRING;
		typeList = new ArrayList<Type>();
		actList = new ArrayList<Act_list>();
		enentsListViewAdapter = new EnentsListViewAdapter(EnentsActivity.this);
		eAdapter = new ActsAdapter(EnentsActivity.this);
		activityListView.setAdapter(eAdapter);
		enents.setAdapter(enentsListViewAdapter);
		activityListView.setAbOnListViewListener(EnentsActivity.this);
	}

	public void showListener() {
		enentsSelect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (enents.getVisibility() == View.GONE) {
					enents.setVisibility(View.VISIBLE);
					enentChooseContent.setChecked(true);
				} else {
					enents.setVisibility(View.GONE);
					enentChooseContent.setChecked(false);
				}
			}
		});
		enents.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {

				String str = enentsListViewAdapter.getTypelist().get(position).getType_name();
				enentAll.setText(str);
				type = copyTypeList.get(position).getType_id();// 保存当前的type
				copyTypeList.clear();
				copyTypeList.addAll(typeList);
				for (int i = 0; i < copyTypeList.size(); i++) {
					if (enentAll.getText().toString().equals(copyTypeList.get(i).getType_name())) {
						copyTypeList.remove(i);
					}
				}
				enentsListViewAdapter.addData(copyTypeList);
				enentsListViewAdapter.notifyDataSetChanged();
				enents.setVisibility(View.GONE);
				enentChooseContent.setChecked(false);
				pageNum = 0;
				if (actList != null)
					actList.clear();
				// 根据改变的type类型去更新下面的mlistView
				loadInfo(type, pageNum, true);
			}
		});

		// 点击活动详情是进入单个活动详情页
		activityListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
				Intent intent = new Intent(EnentsActivity.this, EnentItemActivity.class);
				// 将点击处的ID传递给下一个页面
				intent.putExtra("id", actList.get((int) id).getId());
				startActivity(intent);
			}
		});

	}

	@Override
	public void onRefresh() {
		pageNum = 0;
		loadInfo(type, pageNum, false);

	}

	@Override
	public void onLoadMore() {
		loadInfo(type, ++pageNum, false);
	}

}
